package com.yidu.action;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.model.LockerUser;
import com.yidu.service.UserService;
import com.yidu.util.Message;

import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author roots
 * @since 2018-10-20
 */
@Controller
@RequestMapping("/user")
public class UserController {
	
	@Resource
	private UserService service;
	/**
	 * 查询
	 * @return
	 */
	@RequestMapping("/findAll")
	@ResponseBody
	public List<LockerUser> findAll(LockerUser user) {
		List<LockerUser> list=service.findAll(user);
		for (LockerUser LockerUser : list) {
			System.out.println(LockerUser.getUserId()+"  "+LockerUser.getUserName());
		}
		return list;
	}
	/**
	 * 删除
	 * @param areaId
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public int delete(String userId) {
		return service.delete(userId);
	} 
	/**
	 * 增加或者修改
	 * @param user
	 * @return
	 */
	@RequestMapping("/addOrUpdate")
	@ResponseBody
	public int addOrUpdate(LockerUser user) {
		service.addOrUpdate(user);
		return 0;
	}
	/**
	 * 修改赋值，根据id查询
	 * @param userId
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public LockerUser update(String userId) {
		LockerUser user = service.findById(userId);
		return user;
	}
	/**
	 * 修改
	 */
	@RequestMapping("/updateForm")
	@ResponseBody
	public Message updateForm(LockerUser user) {
		int rows = service.addOrUpdate(user);
		Message msg = new Message();
		if(rows>0) {
			msg.setState(1);
			msg.setMsg("修改成功");
		}else {
			msg.setState(0);
			msg.setMsg("修改成功");
		}
		return msg;
	}
	/**
	 * 根据id查询
	 */
	
	@RequestMapping("/showId")
	@ResponseBody
	public LockerUser showId(HttpSession session) {
		LockerUser lockerUser=(LockerUser) session.getAttribute("user");
		return service.findById(lockerUser.getUserId());
	}
	/**
	 * 根据姓名查询
	 * @param user
	 * @return
	 */
	@RequestMapping("/findName")
	@ResponseBody
	public Message findName(LockerUser user) {
		Message msg = new Message();
		List<LockerUser> list = service.findName(user);
		if(list.size()>0) {
			msg.setState(1);
		}else {
			msg.setState(0);
		}
		return msg;
	}
	@RequestMapping("/regis")
	@ResponseBody
	public Message regis(LockerUser user) { 
		Message msg = new Message();
		if(user.getUserName()==null || "".equals(user.getUserName())) {
			msg.setState(0);
		}else {
			service.add(user);
			msg.setState(1);
		}
		return msg;
	}
	/**
	 * 根据用户名和密码查询
	 * @param admin
	 */
	@RequestMapping("/queryNameOrPwd")
	@ResponseBody
	public Message queryNameOrPwd(LockerUser user,HttpServletRequest request) {
		LockerUser user1 = service.queryNameOrPwd(user);
		Message msg = new Message();
		if(user1 != null) {
			msg.setState(1);
			msg.setMsg("登录成功");
			HttpSession session=request.getSession();
			session.setAttribute("user", user1);
		}else {
			msg.setState(0);
			msg.setMsg("登录失败");
		}
		return msg;
	}
	/**
	 * 获取Seesion
	 * @param request
	 * @return
	 */
	@RequestMapping("/getSessions")
	@ResponseBody
	public LockerUser getSessions(HttpServletRequest request) {
		HttpSession session=request.getSession();
		LockerUser lockerUser=(LockerUser) session.getAttribute("user");
		
		System.out.println("Seesion==="+lockerUser);
		if(lockerUser != null) {
			return lockerUser;
		}else {
			return lockerUser = new LockerUser();
		}
	}
	/**
	 * 取session
	 */
	@RequestMapping("/sessionuser")
	@ResponseBody
	public LockerUser sessionuser(HttpServletRequest request) {
		System.out.println("userseesion");
		HttpSession session=request.getSession();
		LockerUser lockerUser=(LockerUser) session.getAttribute("user");
			return lockerUser;
	}
	
	@RequestMapping("/upjin")
	@ResponseBody
	public int upjin(LockerUser user) {
		System.out.println("进入金额修改:"+user.getUserPhone());
		return service.upjin(user);
	}
}

